//
//  GetCoinViewController.m
//  iMAME4all
//
//  Created by LT-LT on 14-4-2.
//
//

#import "GetCoinViewController.h"
#import "YouMiPointsManager.h"
#import "YouMiWall.h"
#import "MoonGlobalTool.h"
#define SIZE_X    self.view.bounds.size.height/2-75
#define SIZE_Y    self.view.bounds.size.height/2-50


@interface GetCoinViewController ()
@property(nonatomic,strong)UILabel *coinNumLabel;
@property(nonatomic,strong)UILabel *youmiNumLabel;

@end

@implementation GetCoinViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self refreshCoinNumber];
}
-(void)refreshCoinNumber
{
    [_coinNumLabel setText:[[NSUserDefaults standardUserDefaults] valueForKey:@"coin"]];
    int *youmimarks=[YouMiPointsManager pointsRemained];
    [_youmiNumLabel setText:[NSString stringWithFormat:@"%d",*youmimarks]];
    free(youmimarks);
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImageView *backImageview=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Coin_back_image.png"]];
    backImageview.frame=CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
    [self.view addSubview:backImageview];
    [backImageview release];
    
    UILabel *coinLabel =[[UILabel alloc] initWithFrame:CGRectMake(130, 50, 200, 30)];
    [coinLabel setText:@"你目前拥有游戏币："];
    [coinLabel setTextColor:[UIColor whiteColor]];
    [self.view addSubview:coinLabel];
    [coinLabel release];
    
    _coinNumLabel =[[UILabel alloc] initWithFrame:CGRectMake(coinLabel.bounds.size.width+150, 50, 100, 30)];
    [_coinNumLabel setTextColor:[UIColor greenColor]];
    [self.view addSubview:_coinNumLabel];
    
    
    UILabel *youmiLabel=[[UILabel alloc] initWithFrame:CGRectMake(130, 80, 200, 30)];
    [youmiLabel setText:@"你目前拥有积分："];
    [youmiLabel setTextColor:[UIColor whiteColor]];
    [self.view addSubview:youmiLabel];
    [youmiLabel release];
    
    _youmiNumLabel =[[UILabel alloc] initWithFrame:CGRectMake(youmiLabel.bounds.size.width+150, 80, 100, 30)];
    [_youmiNumLabel setTextColor:[UIColor greenColor]];
    [self.view addSubview:_youmiNumLabel];
    
    UIButton *coinButton=[[UIButton alloc] initWithFrame:CGRectMake(SIZE_X, 130, 150, 50)];
    [coinButton setTitle:@"---->获取积分<----" forState:UIControlStateNormal];
    [coinButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [coinButton addTarget:self action:@selector(coinButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:coinButton];
    [coinButton release];
    
    
    UIButton *moreButton=[[UIButton alloc] initWithFrame:CGRectMake(SIZE_X, 170, 150, 50)];
    [moreButton setTitle:@"兑换游戏币" forState:UIControlStateNormal];
    [moreButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [moreButton addTarget:self action:@selector(changeMarkstoCoin) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:moreButton];
    [moreButton release];

    UILabel *tipsLabel=[[UILabel alloc] initWithFrame:CGRectMake(SIZE_X-60, 210, 300, 100)];
    [tipsLabel setText:@"活动消息：下载两个APP并且兑换游戏币成功将额外赠送您【30】个游戏币！"];
    [tipsLabel setTextColor:[UIColor redColor]];
    tipsLabel.lineBreakMode = UILineBreakModeWordWrap;
    tipsLabel.numberOfLines = 0;
    [self.view addSubview:tipsLabel];
    [tipsLabel release];
    
    UIButton *backButton=[[UIButton alloc] initWithFrame:CGRectMake(30, 25, 50, 50)];
    [backButton setTitle:@"退出" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    [backButton release];
	// Do any additional setup after loading the view.
}
-(void)changeMarkstoCoin
{
    int *youmimarks=[YouMiPointsManager pointsRemained];
    if (*youmimarks>=50) {
        int cangetCoinNum=*youmimarks/50;
        NSString *marks=[NSString stringWithFormat:@"你目前拥有积分【%d】,可以兑换【%d】枚游戏币,是否兑换",*youmimarks,cangetCoinNum];
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:marks delegate:self cancelButtonTitle:@"暂不" otherButtonTitles:@"兑换", nil];
        
        [alert show];
        [alert release];
    }else
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"您的积分不够" message:@"50积分可以兑换1游戏币,下载2款APP会额外赠送您30枚游戏币哟" delegate:self cancelButtonTitle:@"马上赚积分" otherButtonTitles:nil, nil];
        alert.tag=990;
        [alert show];
        [alert release];
    }
    free(youmimarks);
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==990) {
        [self coinButtonPressed:nil];
        return;
    }
    if (buttonIndex==1) {

        int *youmimarks=[YouMiPointsManager pointsRemained];
        int cangetCoinNum=((*youmimarks)/50);
        [YouMiPointsManager spendPoints:cangetCoinNum*50];
        int coin =[MoonGlobalTool CoinNumber];
        if (![MoonGlobalTool fristChangeCoin]) {
            coin+=30;
            NSUserDefaults *df =[NSUserDefaults standardUserDefaults];
            [df setObject:@"YES" forKey:@"fristChange"];
            [df synchronize];
        }
        coin+=cangetCoinNum;
        [MoonGlobalTool savecoinNum:coin];
        [self refreshCoinNumber];
    }
    
}
-(void)backButtonPressed:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)coinButtonPressed:(id)sender
{
        [YouMiPointsManager checkPoints];
        int *points = [YouMiPointsManager pointsRemained];
        free(points);
    
        [YouMiWall showOffers:YES didShowBlock:^{
            NSLog(@"有米积分墙已显示");
        } didDismissBlock:^{
            NSLog(@"有米积分墙已退出");
        }];
}

-(void)dealloc
{
    [super dealloc];
    [_coinNumLabel release];
    [_youmiNumLabel release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
